package be.kdg.netpresentadmin;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.*;

/**
 * Special Panel used to show the Questions and enable the admin to select
 * a question he.shre wants to answer. 
 * 
 * @autor Timothy Huber, Joris Clevenbergs, Rik verschuren, Wim Van der Wilt
 */
public class QuestionPanel extends JPanel implements ActionListener {

	/**
	 * Default version ID
	 */
	private static final long serialVersionUID = 1L;
	private Color color;
	private ButtonGroup bgroup;
	
	private ArrayList< QuestionSelectedEventListener > alQnA_Listeners = new ArrayList< QuestionSelectedEventListener >();

	/**
	* Constructor that set the color and size of the panel.
	* 
	* @param color	color of the panel.
	* @param size	dimension of the panel.
	*/
	public QuestionPanel(Color color, Dimension size) {
		this.color = color;
		this.setBackground(color);
	}
	
	/**
	* The next method updates the questions showed in the panel.
	* This is done by first removing all the existing radio-buttons
	* if any, and then redrawing all the newly added questions.
	* 
	* @param questions	vector of strings containing all the questions
	* 					asked over the current slide.
	*/
	public void updatePanel(Vector<String> questions) {
		this.removeAll();
		bgroup = new ButtonGroup();
		this.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		for (int i = 0; i < questions.size(); i++) {
			JRadioButton btn = new JRadioButton();
			btn.setText("" + (i + 1));
			btn.setActionCommand(getName());
			btn.setBackground(color);
			btn.addActionListener(this);
			c.fill = GridBagConstraints.BOTH;
			c.weightx = 1;
			c.gridx = 0;
			c.gridy = i;
			c.insets = new Insets(0, 0, 10, 0); // top padding

			bgroup.add(btn);
			this.add(btn, c);
			JTextArea text = new JTextArea(questions.get(i));
			text.setEditable(false);
			text.setBackground(color);
			text.setLineWrap(true);
			text.setWrapStyleWord(true);

			c.fill = GridBagConstraints.BOTH;
			c.gridx = 1;
			c.gridy = i;
			this.add(text, c);
		}

		this.setBorder(BorderFactory.createTitledBorder(
				BorderFactory.createEtchedBorder(), "Questions:"));
		updateUI();
	}

	/**
	* This method clears the panel of all "radio-button questions"
	* and is used in the above method which issues an update to the panel.
	*/
	public void emptyPanel() {
		removeAll();
		JTextArea text = new JTextArea("Er zijn nog geen vragen");

		text.setEditable(false);
		text.setBackground(color);
		text.setLineWrap(true);
		text.setWrapStyleWord(true);
		this.add(text);
		updateUI();
	}

	/**
	* The next method will return the index of the selected question.
	*/
	private int getSelectedButton() {
		int waarde = 0;
		for (Enumeration<AbstractButton> buttons = bgroup.getElements(); buttons.hasMoreElements();) {
			JRadioButton btn = (JRadioButton) buttons.nextElement();
			if (btn.getModel() == bgroup.getSelection()) {
				waarde = new Integer(btn.getText()).intValue();
			}
		}
		return waarde;
	}

	/**
	* Overriding function that handles the ActionEvents when the button is clicked.
	* 
	* @param e	ActionEvent that has to be handled.
	*/
	public void actionPerformed(ActionEvent e) {
		int tmp = getSelectedButton();
		fireQuestionSelectedEvent( tmp );
	}
	
	/**
	* The next function adds QnSelectedEventListeners to the "alQnA_Listeners" list.
	* 
	* @param l	QnSelectedEventListener that needs to be added to the alQnA_Listeners list.
	*/
	public synchronized void addListener(QuestionSelectedEventListener l) {alQnA_Listeners.add(l); }
	
	/**
	* This function in return removes QnSelectedEventListeners from the "alQnA_Listeners" list.
	* 
	* @param l	QnSelectedEventListener that needs to be removed from the alQnA_Listeners list.
	*/
	public synchronized void removeListener(QuestionSelectedEventListener l) {alQnA_Listeners.remove(l); }
	 
	/**
	* This Method sends QnSelectedEvents to all the listener when a question is selected.
	* A question is "selected" through the activation of a radio-button.
	* 
	* @param id	Integer that contains the id of the selected question.
	*/
	private synchronized void fireQuestionSelectedEvent( int id )
	{
		QuestionSelectedEvent event = new QuestionSelectedEvent( this, id );
		Iterator<QuestionSelectedEventListener> qnansIter = alQnA_Listeners.iterator();
		
		while (qnansIter.hasNext()) { 
			((QuestionSelectedEventListener)qnansIter.next()).questionSelectedEventRecieved(event);
		} 
	}
}
